#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <string>
//多线程
//使用线程创建，线程等待，线程终止，线程分离

// void* start_routine1(void* arg)
// {
//     int* ret = new int(0);
//     // std::cout << "我是线程：" << (char*)arg << std::endl;
//     // pthread_testcancel();
//     pthread_detach(pthread_self());
//     pthread_testcancel();
//     return (void*)ret;
// }
// void* start_routine2(void* arg)
// {
//     int* ret = new int(0);
//     // std::cout << "我是线程：" << (char*)arg << std::endl;
//     // pthread_testcancel();
//     pthread_detach(pthread_self());
//     pthread_testcancel();
//     return (void*)ret;
// }

thread_local int no;
void* start_routine1(void* arg)
{
    no = (long)arg;
    no++;
    sleep(2);
    std::cout << no << std::endl;
    return 0;
}

void* start_routine2(void* arg)
{
    no = (long)arg;
    no++;
    std::cout << no << std::endl;
    return 0;
}
int main()
{
    pthread_t pthread1,pthread2;
    std::string str1 = "pthread1";
    std::string str2 = "pthread2";
    pthread_create(&pthread1,nullptr,start_routine1,(void*)0);
    pthread_create(&pthread2,nullptr,start_routine2,(void*)1);    
    // pthread_detach(pthread1);
    // pthread_detach(pthread2);

    // pthread_cancel(pthread1);
    // pthread_cancel(pthread2);
    // sleep(2);
    // int* val1 = nullptr,*val2 = nullptr;
    // int ret1 = pthread_join(pthread1,(void**)&val1);
    // if(ret1 != 0)
    //     std::cout << str1 << " join error" << std::endl;
    // int ret2 = pthread_join(pthread2,(void**)&val2);
    // if(ret2 != 0)
    //     std::cout << str1 << " join error" << std::endl;

    // std::cout << str1 << ":" << ((void*)val1 == PTHREAD_CANCELED) << std::endl;
    // std::cout << str1 << ":" << ((void*)val2 == PTHREAD_CANCELED) << std::endl;
    pthread_join(pthread1,nullptr);
    pthread_join(pthread2,nullptr);

    
    return 0;
}